<?php

namespace support\login;

use app\services\EmailService;
use app\services\SmsService;
use app\services\UserService;
use plugin\admin\app\model\User;

class EmailLogin implements LoginContract
{

    public function check($data)
    {
        if (!$data['email']) {
            throw new \Exception('邮箱不能为空');
        }

        $user = User::where('email', $data['email'])->first();

        if (empty($data['captcha'])) {
            throw new \Exception('验证码不能为空');
        }

        EmailService::checkCode($data['email'], EmailService::SCENE_LOGIN, $data['captcha']);

        if (!$user) {
            $userId = UserService::register($data);
            $user = UserService::profile($userId);
        }

        return UserService::profile($user['id']);
    }
}